#ifndef _RVJPEG_H_
#define _RVJPEG_H_

class CJPEGDecoder;
class CJPEGEncoder;

#ifdef WIN32
#ifdef MAKEDLL
  #define DLLEXPORT __declspec(dllexport)
#else
  #define DLLEXPORT __declspec(dllimport)
#endif
#else
  #define DLLEXPORT
#endif

#include <vector>

class DLLEXPORT RVJpeg
{
public:
  typedef enum{YCbCr,RGB,BGR,HSV,HLS} DstColorSpace;

  class DstInfo
  {
  public:
    DstInfo( char* buffer_, int size_, DstColorSpace colorSpace_ ) : buffer( buffer_ ), size( size_ ), colorSpace( colorSpace_ ){}
    DstInfo( const DstInfo& src )
    {
      buffer = src.buffer;
      size = src.size;
      colorSpace = src.colorSpace;
    }
    char* buffer;
    int size;
    DstColorSpace colorSpace;
  };

  RVJpeg();
  ~RVJpeg();

  /**Convert the 3 channel RGB data in src to 1 channel gray data stored in dst.
  make sure that:
  sizeof( src ) = srcWidth*srcHeight*3 
  sizeof( dst ) = srcWidth*srcHeight */
  static void convertRGBToGray( const unsigned char* src, int srcWidth, int srcHeight, unsigned char* dst );
  
  bool setDecodeSource( const unsigned char* data, unsigned int length );
  void sourceImageSize( unsigned int* width, unsigned int* height );
  bool setDestination( char* dst, unsigned int dstSize, DstColorSpace dstColorSpace = RGB );
  bool setDestination( std::vector< DstInfo >& dst );
  
  bool decode();

  /** encodes 3 channel RGB to buffer. buffer should be width*height*3, but at least 4096 */
  unsigned int encode( const unsigned char* data, unsigned int width, unsigned int height, unsigned char* buffer, unsigned int bufferSize, unsigned int quality = 75 );

private:
  // decoder
  CJPEGDecoder* _decoder;
  unsigned int _decWidth;
  unsigned int _decHeight;

  CJPEGEncoder* _encoder;
  unsigned int _encWidth;
  unsigned int _encHeight;

  bool readHeader();

#ifdef linux32
  static bool isIppInitialized;
#endif
};

#endif
